function verificateFiled(type) {
	let attr = $(`#${type}i`)
	let val = $(`#${type}`).val()
	let flag = true;
	if (type === 'username') {
		if (val.trim() === '') {
			attr.text('必填，少于6个中文字符或20个英文字符')
			return false;
		}
		flag = /(^[\u4E00-\u9FA5]{2,4}$)|(^([A-Za-z]+\s?)*[A-Za-z]+$)/.test(val)
		if (!flag) attr.text("格式错误")
	} else if (type === 'password') {
		if (val.trim() === '') {
			attr.text('必填，6-16个数字、字母、字符')
			return false;
		}
		flag = /^[a-zA-Z0-9]{6,16}$/.test(val)
		if (!flag) attr.text('6-16位数字、字母')
	} else if (type === 'phone') {
		if (val.trim() === '') {
			attr.text('请填写手机号')
			return false;
		}
		flag = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(val)
		if (!flag) attr.text('请填写正确的手机号')
	} else if (type === 'job') {
		if (val === '请选择职位') {
			attr.text('请选择职位')
			return false;
		}
	} else if (type === 'dept') {
		if (val === '请选择部门') {
			attr.text('请选择部门')
			return false;
		}
	} else if (type === 'birthday') {
		if (val.trim() === '') {
			attr.text('请选择生日')
			return false;
		}
		flag =
			/^((?:19[7-9]\d{1})|(?:20(?:(?:0[0-9])|(?:1[0-8]))))\-((?:0?[1-9])|(?:1[0-2]))\-((?:0?[1-9])|(?:[1-2][0-9])|30|31)$/
			.test(val)
		if (!flag) attr.text("请选择正确的日期")
	}
	if (flag) attr.text("")
	return flag;
}

function randomUid() {
	let s = ''
	for (let i = 0; i < 9; i++) {
		s += Math.floor(Math.random() * 10)
	}
	$('#uid').val(s)
}

function getjobs() {
	axiosRequest1("get", "/job/joblist").then(res => {
		const {
			code,
			data,
			message
		} = res.data
		if (code === 200) {
			let str = '<option>请选择职位</option>'
			data.forEach(item => {
				str += `<option value='${item.jobId}'>${item.jobName}</option>`
			})
			$('#job').html(str)
		} else {
			let warninfo = $('#warninfo')
			warninfo.text(message)
			warninfo.show()
			setTimeout(() => {
				warninfo.hide()
			}, 2000)
		}
	}, error => {
		console.log(error);
	})
}

function getDepts() {
	axiosRequest1("get", "/dept/deptList").then(res => {
		const {
			code,
			data,
			message
		} = res.data
		if (code === 200) {
			let str = '<option value=0>请选择部门</option>'
			data.forEach(item => {
				str += `<option value='${item.deptId}'>${item.deptName}</option>`
			})
			$('#dept').html(str)
		} else {
			let warninfo = $('#warninfo')
			warninfo.text(message)
			warninfo.show()
			setTimeout(() => {
				warninfo.hide()
			}, 2000)
		}
	}, error => {
		console.log(error);
	})

}

function saveUser() {
	if (!verificateFiled("username") || !verificateFiled("password") ||
		!verificateFiled("phone") || !verificateFiled("birthday") ||
		!verificateFiled("dept") || !verificateFiled("job")) {
		return;
	}
	let uId = $('#uid').val()
	let uName = $('#username').val().trim()
	let uPassword = $('#password').val().trim()
	let sex = $('#sex').attr("checked") ? 1 : 2;
	let uTelephone = $('#phone').val().trim()
	let birthday = $('#birthday').val()+' 00:00:00'
	let deptId = $('#dept').attr("value")
	let jobId = $('#job').attr("value")
	let obj = {
		uId,
		uPassword,
		uName,
		sex,
		uTelephone,
		birthday,
		deptId,
		jobId
	}
	axiosRequest1("post", "/user/create", obj).then(res => {
		const {
			code,
			message
		} = res.data
		let warninfo = $('#warninfo')
		warninfo.text(message)
		warninfo.show()
		setTimeout(() => {
			warninfo.hide()
		}, 2000)
		if (code !== 200) randomUid()
	}, error => {
		console.log(error);
	})
}
